package com.huayin.controller;

import com.huayin.model.User;
import com.huayin.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created by huayinmac-002 on 17/1/13.
 */
@Controller
public class IndexController {

    @Autowired
    IndexService indexService;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @PostMapping("/login")
    public  ModelAndView login(@RequestParam String name, @RequestParam String password){
        User user=indexService.findByNameAndPassword(name,password);
        if (user!=null){
            ModelAndView mov = new ModelAndView("/welcome");
            mov.addObject("user", user);
            return mov;
        }else
        {
            ModelAndView mov=new ModelAndView("/login");
            mov.addObject("error","1000");
            return mov;
        }
    }
}
